package novel.spider.impl;

import novel.spider.NovelSiteEnum;
import novel.spider.util.NovelSpiderHttpGet;
import novel.spider.util.NovelSpiderUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

/**
 * 通过一个抽象类抓取内容
 * Created by Kau on 2016/10/31.
 */
public abstract class AbstractSpider {
    protected String crawl(String url) throws Exception{
        /**
         * 创建一个HttpClient实例并接收服务器返回的各种头信息和正文
         */
        // try with resource
        // 谁最后被调用，谁最先被关闭
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build(); CloseableHttpResponse httpResponse = httpClient.execute(new NovelSpiderHttpGet(url))){
            //通过配置文件获取网站字符编码
            String result = EntityUtils.toString(httpResponse.getEntity(), NovelSpiderUtil.getContext(NovelSiteEnum.GetEnumByUrl(url)).get("charset"));
            return result;

        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }


}
